1 using System;
2 using UnityEngine;
3 using UnityStandardAssets.CrossPlatformInput;
4
5 namespace UnityStandardAssets.Vehicles.Aeroplane
6 {
7 [RequireComponent(typeof (AeroplaneController))]
8 public class AeroplaneUserControl4Axis : MonoBehaviour
9 {
10 // these max angles are only used on mobile, due to the way pitch and roll input are handled
11 public float maxRollAngle = 80;
12 public float maxPitchAngle = 80;
13
14 // reference to the aeroplane that we're controlling
15 private AeroplaneController m_Aeroplane;
16 private float m_Throttle;
17 private bool m_AirBrakes;
18 private float m_Yaw;
19
20
21 private void Awake()
22 {
23 // Set up the reference to the aeroplane controller.
24 m_Aeroplane = GetComponent<AeroplaneController>();
25 }
26
27
28 private void FixedUpdate()
29 {
30 // Read input for the pitch, yaw, roll and throttle of the aeroplane.
31 float roll = CrossPlatformInputManager.GetAxis("Mouse X");
32 float pitch = CrossPlatformInputManager.GetAxis("Mouse Y");
33 m_AirBrakes = CrossPlatformInputManager.GetButton("Fire1");
34 m_Yaw = CrossPlatformInputManager.GetAxis("Horizontal");
35 m_Throttle = CrossPlatformInputManager.GetAxis("Vertical");
36 #if MOBILE_INPUT
37 AdjustInputForMobileControls(ref roll, ref pitch, ref m_Throttle);
38 #endif
39 // Pass the input to the aeroplane
40 m_Aeroplane.Move(roll, pitch, m_Yaw, m_Throttle, m_AirBrakes);
41 }
42
43
44 private void AdjustInputForMobileControls(ref float roll, ref float pitch, ref float throttle)
45 {
46 // because mobile tilt is used for roll and pitch, we help out by
47 // assuming that a centered level device means the user
48 // wants to fly straight and level!
49
50 // this means on mobile, the input represents the *desired* roll angle of the aeroplane,
51 // and the roll input is calculated to achieve that.
52 // whereas on non-mobile, the input directly controls the roll of the aeroplane.
53
54 float intendedRollAngle = roll*maxRollAngle*Mathf.Deg2Rad;
55 float intendedPitchAngle = pitch*maxPitchAngle*Mathf.Deg2Rad;
56 roll = Mathf.Clamp((intendedRollAngle - m_Aeroplane.RollAngle), -1, 1);
57 pitch = Mathf.Clamp((intendedPitchAngle - m_Aeroplane.PitchAngle), -1, 1);
58 }
59 }
60 }